package com.matrix.marketing.init;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.matrix.marketing.entity.CouponTemplate;
import com.matrix.marketing.service.CouponTemplateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 有点甜
 * @since 2025/8/1
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CouponTemplateInitializer implements CommandLineRunner {

    private final CouponTemplateService couponTemplateService;

    @Override
    public void run(String... args) {
        log.info("开始初始化平台优惠券模板...");

        List<CouponTemplate> templates = List.of(
                createTemplate(1L, "满20减5元", 1, new BigDecimal("20.00"), new BigDecimal("5.00"), 7),
                createTemplate(2L, "满10减3元", 1, new BigDecimal("10.00"), new BigDecimal("3.00"), 7),
                createTemplate(3L, "满24减6元", 1, new BigDecimal("24.00"), new BigDecimal("6.00"), 7),
                createTemplate(4L, "满30减8元", 1, new BigDecimal("30.00"), new BigDecimal("8.00"), 7),
                createTemplate(5L, "满15减3元", 1, new BigDecimal("15.00"), new BigDecimal("3.00"), 7),
                createTemplate(6L, "满100减30元", 1, new BigDecimal("100.00"), new BigDecimal("30.00"), 7),
                createTemplate(7L, "满50减10元", 1, new BigDecimal("50.00"), new BigDecimal("10.00"), 7),
                createTemplate(8L, "满38减18元", 1, new BigDecimal("38.00"), new BigDecimal("18.00"), 7),
                createTemplate(9L, "新用户专享-满20减18", 1, new BigDecimal("20.00"), new BigDecimal("18.00"), 7),
                createTemplate(10L, "积分兑换-无门槛7元", 3, BigDecimal.ZERO, new BigDecimal("7.00"), 30),
                createTemplate(11L, "积分兑换-无门槛15元", 3, BigDecimal.ZERO, new BigDecimal("15.00"), 30),
                createTemplate(12L, "积分兑换-无门槛30元", 3, BigDecimal.ZERO, new BigDecimal("30.00"), 30)
        );

        for (CouponTemplate template : templates) {
            long count = couponTemplateService.count(
                    new LambdaQueryWrapper<CouponTemplate>().eq(CouponTemplate::getId, template.getId())
            );
            if (count == 0) {
                couponTemplateService.save(template);
                log.info("已创建优惠券模板: {}", template.getTitle());
            }
        }
        log.info("平台优惠券模板初始化完成。");
    }

    private CouponTemplate createTemplate(Long id, String title, int type, BigDecimal threshold, BigDecimal discount, int validDays) {
        CouponTemplate t = new CouponTemplate();
        t.setId(id);
        t.setIssuerType(1);
        t.setIssuerId(0L);
        t.setTitle(title);
        t.setCouponType(type);
        t.setThresholdAmount(threshold);
        t.setDiscountAmount(discount);
        t.setTotalQuantity(-1);
        t.setValidityType(1);
        t.setValidDays(validDays);
        t.setStatus(1);
        return t;
    }
}
